package com.zxy.codeserver;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.zxy.common.EmailRemoteCall;
import com.zxy.common.User;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
 * @author zhangxuyuan
 * @version 1.0
 * @date 2021/1/27
 */
@RestController
public class CodeController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Reference
    private EmailRemoteCall emailRemoteCall;


    @GetMapping("/create/{email}")
    public Boolean create(@PathVariable("email")String email)
    {
        Random random = new Random();
        int i = random.nextInt(8999);
        String code = 1000+i+"";
        DateTime dateTime = DateUtil.dateSecond();
        dateTime=DateUtil.offset(dateTime, DateField.MINUTE,5);
       try {
           emailRemoteCall.send(email, code);
           User user = new User(email,null,code,dateTime,null);
           redisTemplate.opsForValue().set(email,user);
           return true;
       }catch (Exception e)
       {
           e.printStackTrace();
           return false;
       }
    }
}
